<?php
/**
 +-----------------------------------------------------------------------+
 | dcAlbums - a Flickr plugin for Dotclear 2                             |
 | Copyright (C) 2008 - France                                           |
 |                                                                       |
 | This program is free software: you can redistribute it and/or modify  |
 | it under the terms of the GNU General Public License as published by  |
 | the Free Software Foundation, version 2 of the License.               |
 |                                                                       |
 | This program is distributed in the hope that it will be useful,       |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of        |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
 | GNU General Public License for more details.                          |
 |                                                                       |
 | You should have received a copy of the GNU General Public License     |
 | along with this program.  If not, see <http://www.gnu.org/licenses/>. |
 |                                                                       |
 +-----------------------------------------------------------------------+
 |  Author : Erwan Richard <contact@erwan-richard.info>                  |
 +-----------------------------------------------------------------------+ 
*/

require (dirname(__FILE__).'/phpFlickr/phpFlickr.php');
function array2object(array $array) {
	$object = new stdClass();
	foreach($array as $key => $value) {
		if(is_array($value)) {
			$object->$key = array2object($value);
		} else {
			$object->$key = $value;
		}
	}
	return $object;
}
	
class dcAlbums extends dcMedia
{
	public $api;

	public function __construct(&$core)
	{
		if (empty($core->phpFlickr)) {
			$api_key = $core->blog->settings->dcAlbums_api_key;
			$secret = $core->blog->settings->dcAlbums_secret;
		}
		$this->api = new phpFlickr($api_key,$secret);
		$this->api->setToken($core->blog->settings->dcAlbums_user_token);
		$this->api->enableCache('fs',DC_TPL_CACHE.'/phpflickr');
		
		parent::__construct($core);
	}
	
	public function getAlbums($params=array(),$count_only=false){
		$list = $this->api->photosets_getList();
		
		foreach ($list['photoset'] as &$ps):
			$ps['url'] = $this->core->blog->url.$this->core->blog->settings->dcAlbums_album_url_prefix.'/'.$ps['id'];
		endforeach; 
		
		return array2object($list['photoset']);
	}
	
	public function getPhotos($params=array(),$count_only=false){
		
		$photos = $this->api->photosets_getPhotos(
			$params['photoset_id'], $params['extras'],$params['privacy_filter'],
			$params['per_page'],$params['page']);
		 
		
		return array2object($photos);
	}
	
	public function getAlbum($params=array()){
		$lists = $this->getAlbums();
		
		foreach($lists as $ps):
			if ($params['photoset_id'] == $ps->id):
				$photoset = $ps;
				$photoset->photoslist = $this->getPhotos($params);
				
				foreach($photoset->photoslist->photo as &$p):
					$fp = $this->api->photos_getInfo($p->id);
					$p->description = $fp['description'];
				endforeach;
			endif;	
		endforeach;
		
		return $photoset;
	}
}
?>
